package arm.admin.bean;

import arm.admin.entity.Users;
import arm.common.manager.TableManager;
import arm.common.tools.Tools;
import arm.db.tools.DBManager;

public class UsersBean
{
    private Users user;
    private TableManager<Users> tableManager;

    public Users getUsers() {
        return user;
    }

    public void setUsers(Users user) {
        this.user = user;
    }

    public String viewTable ()
    {
        getTableManager().readFromDB();
        return "viewUsersTable" ;
    }

    public TableManager<Users> getTableManager () {
        if (tableManager==null)
        {
            tableManager=new TableManager<Users>(Users.class);
        }
        return tableManager;
    }

    public void setTableManager (TableManager<Users> tableManager) {
        this.tableManager=tableManager;
    }

    public String create()
    {
        user = new Users();
        return "editUsers";
    }

    public String save()
    {
        String newPassword = Tools.md5AsHexString(getUsers().getPassword());
        if(getUsers().getId() == null || !getUsers().getPassword().equals(newPassword))
            getUsers().setPassword(newPassword);
        DBManager.saveOrUpdate(getUsers());
        return viewTable();
    }

    public String edit () {
        setUsers(getTableManager().getSelectedObj());
        return "editUsers";
    }

    public  String cancel () {
        return viewTable();
    }

    public void delete () {
        DBManager.delete(getTableManager().getSelectedObj());
        getTableManager().readFromDB();
    }

}
